package com.wondertek.service.service.business.impl;

import com.wondertek.common.exception.AppException;
import com.wondertek.common.util.base.StringUtil;
import com.wondertek.common.util.security.LoginUser;
import com.wondertek.common.util.security.SecurityUtil;
import com.wondertek.service.dto.app.req.HomePageDateReq;
import com.wondertek.service.dto.app.res.HomePageRoomRes;
import com.wondertek.service.dto.app.res.HomePageTopRes;
import com.wondertek.service.dto.app.res.HomePageTrendRes;
import com.wondertek.service.dto.app.res.HomePageTypeRes;
import com.wondertek.service.mapper.business.HomePageMapper;
import com.wondertek.service.service.business.HomePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author wangyu
 */
@Service
public class HomePageServiceImpl implements HomePageService {

//    @Autowired
//    private DhDeviceService dhDeviceService;

    @Autowired
    private HomePageMapper homePageMapper;

    /**
     * 首页顶部数量查询
     */
    @Override
    public HomePageTopRes findTop() {

        LoginUser loginUser = SecurityUtil.getLoginUser();

        String userId = loginUser != null ? loginUser.getUserId() : null;

        if (StringUtil.isEmpty(userId))
            throw new AppException("user.id.not.null");

        HomePageTopRes top = homePageMapper.top(userId);

        List<Integer> deviceCodes = homePageMapper.topDeviceCode(userId);
        int onLineSum = 0;
        int offLineSum = 0;
   //     if (deviceCodes != null && !deviceCodes.isEmpty()) {
//            DeviceListReq deviceListReq = new DeviceListReq();
//            deviceListReq.setDeviceIds(deviceCodes);
//            Result<DevicePageRes> devicePageResResult = dhDeviceService.pageDevice(deviceListReq);
//            DevicePageRes devicePageRes = JSONObject.parseObject(String.valueOf(devicePageResResult.getData()), DevicePageRes.class);
//            List<DeviceRes> records = devicePageRes.getRecords();
            for (Integer d : deviceCodes) {
                if (d == 1)
                    onLineSum++;
                else
                    offLineSum++;
            }

   //     }

        List<Integer> accessoryCodes = homePageMapper.topAccessoryCode(userId);

//        if (accessoryCodes != null && !accessoryCodes.isEmpty()) {
  //          GetAccessoryInfoReq getAccessoryInfoReq = new GetAccessoryInfoReq();
       //     getAccessoryInfoReq.setDeviceId();
    //        getAccessoryInfoReq.setAccessoryIds(accessoryCodes);
      //      Result<List<GetAccessoryInfoRes>> accessoryInfo = dhDeviceService.getAccessoryInfo(getAccessoryInfoReq);
        //    List<GetAccessoryInfoRes> records = accessoryInfo.getData();
       //     List<DeviceRes> records = devicePageRes.getRecords();
            for (Integer d : accessoryCodes) {

                if (d == 1)  // 1在线
                    onLineSum++;
                else
                    offLineSum++;
            }




      //  }

        top.setOnLineSum(onLineSum);
        top.setOffLineSum(offLineSum);

        return top;
    }

    /**
     * 房间控制
     *
     * @return
     */
    @Override
    public List<HomePageRoomRes> findRoom() {
        LoginUser loginUser = SecurityUtil.getLoginUser();

        String userId = loginUser != null ? loginUser.getUserId() : null;

        if (StringUtil.isEmpty(userId))
            throw new AppException("user.id.not.null");


        List<HomePageRoomRes> room = homePageMapper.findRoom(userId);

//        for (HomePageRoomRes r : room){
//            GetAreaInfosReq getAreaInfosReq = new GetAreaInfosReq();
//            getAreaInfosReq.setDeviceId(r.getSnCode());
//            getAreaInfosReq.setAreaId(r.getRoomCode());
//            Result<GetAreaArmModeRes> areaArmMode = null;
//            try {
//                areaArmMode = dhDeviceService.getAreaArmMode(getAreaInfosReq);
//
//            }catch (Exception e){
//                return room;
//            }
//
//      //      ArmModesRes armModesRes = JSONObject.parseObject(String.valueOf(areaArmMode.getData()), ArmModesRes.class);
//
//            ArmModesRes armModesRes = null;
//            try {
//                List<ArmModesRes> armModes = JSONArray.parseArray(JSONObject.parseObject(String.valueOf(areaArmMode.getData())).get("armModes").toString(), ArmModesRes.class);
//                if(armModes != null && !armModes.isEmpty())
//                    armModesRes = armModes.get(0);
//            }catch (Exception e){
//                throw new AppException("调用大华异常");
//            }
//
//         //   ArmModesRes armModes = arm
//            r.setMode(armModesRes.getMode());
//        }

//        Set<String> snCodes = new HashSet<>();
//
//        for (int i = 0; i < room.size(); i++) {
//            snCodes.add(room.get(i).getSnCode());
//        }
//
//        for (String s : snCodes) {
//            GetAreaInfosReq getAreaInfosReq = new GetAreaInfosReq();
//            getAreaInfosReq.setDeviceId(s);
//            Result<GetAreaArmModeRes> areaArmMode = dhDeviceService.getAreaArmMode(getAreaInfosReq);
//            List<ArmModesRes> armModes = JSONArray.parseArray(JSONObject.parseObject(String.valueOf(areaArmMode.getData())).get("armModes").toString(), ArmModesRes.class);
//
//            for (){
//
//            }
//            //     GetAreaArmModeRes getAreaArmModeRes = JSONObject.parseObject(String.valueOf(areaArmMode.getData()), GetAreaArmModeRes.class);
//        }


        return room;
    }

    /**
     * 首页报警趋势
     *
     * @param homePageDateReq
     * @return
     */
    @Override
    public List<HomePageTrendRes> findTrend(HomePageDateReq homePageDateReq) {
        LoginUser loginUser = SecurityUtil.getLoginUser();

        String userId = loginUser != null ? loginUser.getUserId() : null;

        if (StringUtil.isEmpty(userId))
            throw new AppException("user.id.not.null");

        homePageDateReq.setConsumerId(userId);

        return homePageMapper.findTrend(homePageDateReq);
    }

    @Override
    public List<HomePageTypeRes> findTypeRatio(HomePageDateReq homePageDateReq) {
        LoginUser loginUser = SecurityUtil.getLoginUser();

        String userId = loginUser != null ? loginUser.getUserId() : null;

        if (StringUtil.isEmpty(userId))
            throw new AppException("user.id.not.null");

        homePageDateReq.setConsumerId(userId);

        List<HomePageTypeRes> typeRatio = homePageMapper.findTypeRatio(homePageDateReq);

        int sum = 0;

        for (int i = 0; i < typeRatio.size(); i++) {
            sum += typeRatio.get(i).getNum();
        }


        if (sum != 0) {
            for (int i = 0; i < typeRatio.size(); i++) {
                HomePageTypeRes homePageTypeRes = typeRatio.get(i);
                homePageTypeRes.setRatio((int) Math.round((double) homePageTypeRes.getNum() / sum * 100));
            }
        }
        return typeRatio;
    }

}

